package com.raja.servingStation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.Constants;
import com.raja.servingStation.common.util.ReturnCodes;
import com.raja.servingStation.dao.OrderClaimDao;
import com.raja.servingStation.entity.ComponentModel;
import com.raja.servingStation.entity.FaultDiacrisis;
import com.raja.servingStation.entity.RepairOrder;
import com.raja.servingStation.entity.RepairPlan;
import com.raja.servingStation.service.OrderClaimService;
import com.raja.servingStation.service.TbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class OrderClaimServiceImpl implements OrderClaimService {

    @Autowired
    private OrderClaimDao orderClaimDao;
    @Autowired
    private TbOrderService tbOrderService;

    @Override
    public ReturnCodes commitOrderClaim(JSONObject parseBody) throws Exception {
        Integer orderId = parseBody.getInteger("orderId");
        Integer orderType = parseBody.getInteger("orderType");
        int orderCount = tbOrderService.queryOrderIsExit(orderId,orderType);
        if(orderCount !=1)
            return ReturnCodes.ORDER_IS_NOT_EXIT;
        //查询此维修单是否存在索赔记录未完成的；如果有不能进行索赔
        int count = orderClaimDao.getOrderClaimCountById(orderId,orderType);
        if(count != 0) {
            if(orderType == 1)
                return ReturnCodes.CLAIM_IS_NOT_OVER;
            if(orderType == 2)
                return ReturnCodes.FIRST_INSURANCE_ORDER_IS_ONLY_ONE;
        }
        //如果是维修工单
        if(orderType == 1){
            //查询故障诊断
            FaultDiacrisis faultDiacrisis = tbOrderService.getFaultDiacrisisByOrderId(orderId);
            if(faultDiacrisis == null) return ReturnCodes.ORDER_NO_FAULT_DIACRISIS;
            if(faultDiacrisis != null && faultDiacrisis.getBlameType() != Constants.BLAME_TYPE_11){
                List<ComponentModel> componentModels = tbOrderService.getComponentsByOrderId(orderId);
                if(componentModels == null || componentModels.size() == 0)
                    return ReturnCodes.ORDER_NO_COMPONENT;
                else {
                    boolean isOk = false;
                    for (int i = 0;i<componentModels.size();i++){
                        ComponentModel componentModel = componentModels.get(i);
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(componentModel.getKpTime());
                        calendar.add(Calendar.MONTH,componentModel.getGuaranteeMonth());
                        //如果当期里程小于保养里程，并且零件保养周期在设置的范围内，即可索赔
                        if(componentModel.getCurrentTotalKm() <= componentModel.getGuaranteeMileage() && System.currentTimeMillis() <= calendar.getTimeInMillis()){
                            isOk = true;
                            break;
                        }
                    }
                    if(!isOk)
                        return ReturnCodes.CLAIM_RANGE_OVER;
                    else {
                        orderClaimDao.commitOrderClaim(orderId, orderType);
                        return ReturnCodes.HANDLE_SUCCESS;
                    }
                }
            }else {
                return ReturnCodes.CLAIM_TYPE_IS_ERROR;
            }
        }else  if(orderType == 2){
            //如果是首保单
            orderClaimDao.commitOrderClaim(orderId,orderType);
            return ReturnCodes.HANDLE_SUCCESS;
        }else {
                return null;
        }
    }

}
